package club.yuit.desktop.editor;

import lombok.extern.slf4j.Slf4j;

import javax.swing.*;
import javax.swing.text.*;
import java.awt.*;

/**
 * @author: yuit
 * @date: 2023/12/11 20:16
 */
@Slf4j
public class LogEditorPane extends JTextPane {

    private StyledDocument document;
    private Element root;

    public LogEditorPane() {
        document = this.getStyledDocument();
        root = document.getDefaultRootElement();
        this.setBackground(Color.WHITE);
        this.insertValueDefaultAttr("-----------------日志内容区域--------------\n",Color.RED);
    }

    public void insertValueDefaultAttr(String value) {
        insertValue(value, document.getLength(), Color.BLACK);
    }

    public void insertValueDefaultAttr(String value, Color color) {
        insertValue(value, document.getLength(), color);
    }

    public void insertValueSplitEnd(String value) {
        insertValue(value, document.getLength(), Color.GRAY);
    }

    public void insertValueMSG(String value) {
        insertValue(value, document.getLength(), Color.GRAY);

    }


    public void insertValueRES(String value) {
        insertValue(value, document.getLength(), Color.BLUE);
    }


    /**
     * 指定位置插入值
     *
     * @param value
     * @param offset
     * @param color
     */
    public void insertValue(String value, int offset, Color color) {
        if (value != null && !value.isEmpty()) {
            SimpleAttributeSet attr = new SimpleAttributeSet();
            StyleConstants.setForeground(attr, color);
            try {
                document.insertString(offset, value, attr);
                document.setParagraphAttributes(offset+1,value.length(),attr,false);
                //log.debug("offset:{},panelLength: {},docLength: {},valueLength: {}",offset,this.getText().length(),document.getLength(),value.length());
                this.setCaretPosition(document.getLength());
            } catch (BadLocationException e) {
                throw new RuntimeException(e);
            }

        }
    }

    public void insertValue(String value, Color color) {
        insertValue(value, document.getLength(), color);
    }


    public void insertGrayValue(String value) {
        insertValue(value, document.getLength(), Color.GRAY);
    }

}
